// Copyright by LunaSec (owned by Refinery Labs, Inc)
//
// Licensed under the Business Source License v1.1
// (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
// https://github.com/lunasec-io/lunasec/blob/master/licenses/BSL-LunaTrace.txt
//
// See the License for the specific language governing permissions and
// limitations under the License.
package npm

import (
	"encoding/json"
	"github.com/lunasec-io/lunasec/lunatrace/bsl/ingest-worker/pkg/metadata"
)

func ParseRawPackageMetadata(pkgMetaRaw []byte) (*metadata.PackageMetadata, error) {
	var pkgMeta NpmPackageMetadataWithRawVersions
	err := json.Unmarshal(pkgMetaRaw, &pkgMeta)
	if err != nil {
		return nil, err
	}

	var pkgMetaForDB NpmPackageMetadata
	err = json.Unmarshal(pkgMetaRaw, &pkgMetaForDB)
	if err != nil {
		return nil, err
	}
	pkgMetaForDBRaw, err := json.Marshal(&pkgMetaForDB)
	if err != nil {
		return nil, err
	}

	return adapt(&pkgMeta, pkgMetaForDBRaw)
}
